package com.application.fabric.test.picverify;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

@RestController
@RequestMapping("/pic")
public class piccol {

    @Autowired
    private  picentity picentity;
    @Autowired
    private picmain picmain;
    @CrossOrigin
    @GetMapping("img")
    public  ResponseEntity getimg() throws IOException {
        // 将BufferedImage对象转换为字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(picmain.getmsg(), "png", baos);
        baos.flush();
        byte[] imageBytes = baos.toByteArray();
        baos.close();

        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);

//        picentity.setCode();
        ResponseEntity r = new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
        picentity.setImg(r);
        // 返回图片数据
        picentity.setCode("");
        picentity.setCode(picmain.getcode());
        System.out.println(picentity.getCode());
        return r;
    }

    @CrossOrigin
    @GetMapping("code")
    public String getcode(){
        return picentity.getCode();
    }
}
